﻿using CommunityToolkit.Mvvm.Input;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace AppTemplate.UI.Controls
{
    [TemplatePart(Name = PART_PageSizes, Type = typeof(ComboBox))]
    public class Pager : Control
    {
        const string PART_PageSizes = "PART_PageSizes";

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            var cb = this.Template.FindName(PART_PageSizes, this) as ComboBox;
            if (cb != null)
            {
                cb.SelectedIndex = 0;
            }
        }


        public int TotalCount
        {
            get
            {
                return (int)GetValue(TotalCountProperty);
            }
            set
            {
                SetValue(TotalCountProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for TotalCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TotalCountProperty =
            DependencyProperty.Register(nameof(TotalCount), typeof(int), typeof(Pager));


        public static readonly DependencyProperty PageSizesProperty = DependencyProperty.Register(nameof(PageSizes), typeof(List<int>), typeof(Pager));
        public List<int> PageSizes
        {
            get
            {
                return (List<int>)GetValue(PageSizesProperty);
            }
            set
            {
                SetValue(PageSizesProperty, value);
            }
        }



        public int PageCount
        {
            get { return (int)GetValue(PageCountProperty); }
            set { SetValue(PageCountProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PageCount.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PageCountProperty =
            DependencyProperty.Register(nameof(PageCount), typeof(int), typeof(Pager));








        public int PageSize
        {
            get { return (int)GetValue(PageSizeProperty); }
            set { SetValue(PageSizeProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PageSize.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PageSizeProperty =
            DependencyProperty.Register(nameof(PageSize), typeof(int), typeof(Pager));




        public int CurrentPage
        {
            get { return (int)GetValue(CurrentPageProperty); }
            set { SetValue(CurrentPageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for CurrentPage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CurrentPageProperty =
            DependencyProperty.Register(nameof(CurrentPage), typeof(int), typeof(Pager), new PropertyMetadata(0));


        public static readonly DependencyProperty JumpPageCommandProperty = DependencyProperty.Register(nameof(JumpPageCommand), typeof(AsyncRelayCommand<object>), typeof(Pager));
        public AsyncRelayCommand<object> JumpPageCommand
        {
            get
            {
                return (AsyncRelayCommand<object>)GetValue(JumpPageCommandProperty);
            }
            set
            {
                SetValue(JumpPageCommandProperty, value);
            }
        }




        public AsyncRelayCommand<int> PageSizeChangedCommand
        {
            get { return (AsyncRelayCommand<int>)GetValue(PageSizeChangedCommandProperty); }
            set { SetValue(PageSizeChangedCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PageSizeChangedCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PageSizeChangedCommandProperty =
            DependencyProperty.Register(nameof(PageSizeChangedCommand), typeof(AsyncRelayCommand<int>), typeof(Pager));


    }
}
